package com.medishare.activity;

import java.io.IOException;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import publicutils.MediUrl;
import publicutils.WeatherInfo;

import com.medishare.R;
import com.medishare.http.HttpUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivityWeatherNotice extends Activity{
	private ImageView titleImg;
	private TextView titleText;
	private TextView city;
	private TextView sd;
	private TextView winds;
	private TextView suggest;
	private TextView week;
	private TextView week2;
	private TextView week3;
	private TextView week4;
	private TextView week5;
	private TextView weather;
	private TextView weather2;
	private TextView weather3;
	private TextView weather4;
	private TextView weather5;
	private TextView temp;
	private TextView temp2;
	private TextView temp3;
	private TextView temp4;
	private TextView temp5;
	private TextView tempTitle2;
	private TextView tempTitle3;
	private TextView tempTitle4;
	private TextView tempTitle5;
	private ImageView img1;
	private ImageView img2;
	private ImageView img3;
	private ImageView img4;
	private ImageView img5;
	private ImageView img6;
	private ImageView img7;
	private ImageView img8;
	private ImageView img9;
	private ImageView img10;
	private TextView fail;
	private LinearLayout midLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.weather_notice);
		findViews();
		initTitle();
		
		new AsyncTask<String, Void, WeatherInfo>(){
			protected void onPreExecute() {
				fail.setText(R.string.caching);
			};
			@Override
			protected WeatherInfo doInBackground(String... params) {
				// TODO Auto-generated method stub			
				String result1 =  HttpUtils.getReponseByHttpGet(MediUrl.weatherUrl, 30000);
				String result2 =  HttpUtils.getReponseByHttpGet(MediUrl.weatherUrl2, 30000);
				WeatherInfo wInfo = new WeatherInfo();
				if (result1.equals("timeout") || result1.equals("fail")
						|| result2.equals("timeout") || result2.equals("fail")){
					return null;
				}
				try {
					JSONObject object = new JSONObject(result1);
					JSONObject object2 = new JSONObject(result2);
					JSONObject wearherInfo = object.getJSONObject("weatherinfo");
					JSONObject wearherInfo2 = object2.getJSONObject("weatherinfo");
					wInfo.city = wearherInfo.getString("city");
					wInfo.week = wearherInfo2.getString("week");
					wInfo.temp = wearherInfo.getString("temp");
					wInfo.sd = wearherInfo.getString("SD");
					wInfo.winds = wearherInfo.getString("WD");
					wInfo.suggest = wearherInfo2.getString("index_d");
					wInfo.temp2 = wearherInfo2.getString("temp2");
					wInfo.temp3 = wearherInfo2.getString("temp3");
					wInfo.temp4 = wearherInfo2.getString("temp4");
					wInfo.temp5 = wearherInfo2.getString("temp5");
					
					wInfo.weather = wearherInfo2.getString("weather1");
					wInfo.weather2 = wearherInfo2.getString("weather2");
					wInfo.weather3 = wearherInfo2.getString("weather3");
					wInfo.weather4 = wearherInfo2.getString("weather4");
					wInfo.weather5 = wearherInfo2.getString("weather5");
					
					wInfo.img1 = wearherInfo2.getString("img1");
					wInfo.img2 = wearherInfo2.getString("img2");
					wInfo.img3 = wearherInfo2.getString("img3");
					wInfo.img4 = wearherInfo2.getString("img4");
					wInfo.img5 = wearherInfo2.getString("img5");
					wInfo.img6 = wearherInfo2.getString("img6");
					wInfo.img7 = wearherInfo2.getString("img7");
					wInfo.img8 = wearherInfo2.getString("img8");
					wInfo.img9 = wearherInfo2.getString("img9");
					wInfo.img10 = wearherInfo2.getString("img10");
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return wInfo;
			}
			
			protected void onPostExecute(WeatherInfo result) {
				if (result != null) {
					midLayout.setVisibility(View.GONE);
					initViews(result);
				}else {
					fail.setText(R.string.fail);
				}
			};		
			
		}.execute();
			
			
	}
	
	private void findViews(){
		titleImg = (ImageView) findViewById(R.id.titleImg);
		titleText = (TextView) findViewById(R.id.title);
		
		midLayout = (LinearLayout) findViewById(R.id.midLayout);
		fail = (TextView) findViewById(R.id.fail);
		
		city = (TextView) findViewById(R.id.city);
		sd = (TextView) findViewById(R.id.sd);
		winds = (TextView) findViewById(R.id.winds);
		suggest = (TextView) findViewById(R.id.suggest);
		
		week = (TextView) findViewById(R.id.week);
		week2 = (TextView) findViewById(R.id.week2);
		week3 = (TextView) findViewById(R.id.week3);
		week4 = (TextView) findViewById(R.id.week4);
		week5 = (TextView) findViewById(R.id.week5);
		
		temp = (TextView) findViewById(R.id.temp);
		temp2 = (TextView) findViewById(R.id.temp2);
		temp3 = (TextView) findViewById(R.id.temp3);
		temp4 = (TextView) findViewById(R.id.temp4);
		temp5 = (TextView) findViewById(R.id.temp5);
		
		tempTitle2 = (TextView) findViewById(R.id.tempTitle2);
		tempTitle3 = (TextView) findViewById(R.id.tempTitle3);
		tempTitle4 = (TextView) findViewById(R.id.tempTitle4);
		tempTitle5 = (TextView) findViewById(R.id.tempTitle5);
		
		weather = (TextView) findViewById(R.id.weather);
		weather2 = (TextView) findViewById(R.id.weather2);
		weather3 = (TextView) findViewById(R.id.weather3);
		weather4 = (TextView) findViewById(R.id.weather4);
		weather5 = (TextView) findViewById(R.id.weather5);
		
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		img3 = (ImageView) findViewById(R.id.img3);
		img4 = (ImageView) findViewById(R.id.img4);
		img5 = (ImageView) findViewById(R.id.img5);
		img6 = (ImageView) findViewById(R.id.img6);
		img7 = (ImageView) findViewById(R.id.img7);
		img8 = (ImageView) findViewById(R.id.img8);
		img9 = (ImageView) findViewById(R.id.img9);
		img10 = (ImageView) findViewById(R.id.img10);
		
	}
	
	private void initTitle(){
//		titleImg.setBackgroundResource(R.drawable.weather_info);
//		titleText.setText(R.string.weather_notice);
	}
	
	private void initViews(WeatherInfo wInfo){		
		city.setText(wInfo.city);
		sd.setText("湿度  : " + wInfo.sd);
		winds.setText("风向  : " + wInfo.winds);
		suggest.setText("建议 : " + wInfo.suggest);
		
		week.setText("(" + wInfo.week + ")");
		String[] weekDays = weekDays(matchDayofWeek(wInfo.week));
		week2.setText(weekDays[0]);
		week3.setText(weekDays[1]);
		week4.setText(weekDays[2]);
		week5.setText(weekDays[3]);
		
		temp.setText("温度  : " + wInfo.temp + "°C");
		temp2.setText(wInfo.temp2);
		temp3.setText(wInfo.temp3);
		temp4.setText(wInfo.temp4);
		temp5.setText(wInfo.temp5);
		
		tempTitle2.setText(R.string.temp);
		tempTitle3.setText(R.string.temp);
		tempTitle4.setText(R.string.temp);
		tempTitle5.setText(R.string.temp);
		
		weather.setText("天气 ：" + wInfo.weather);
		weather2.setText(wInfo.weather2);
		weather3.setText(wInfo.weather3);
		weather4.setText(wInfo.weather4);
		weather5.setText(wInfo.weather5);
		
		img1.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img1 + ".png"));
		if (wInfo.img2.equals("99")){
			img2.setVisibility(View.GONE);
		}else {
			img2.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img2 + ".png"));
		}
		
		img3.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img3 + ".png"));
		if (wInfo.img4.equals("99")){
			img4.setVisibility(View.GONE);
		}else {
			img4.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img4 + ".png"));
		}
		
		img5.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img5 + ".png"));
		if (wInfo.img6.equals("99")){
			img6.setVisibility(View.GONE);
		}else {
			img6.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img6 + ".png"));
		}
		
		img7.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img7 + ".png"));
		if (wInfo.img8.equals("99")){
			img8.setVisibility(View.GONE);
		}else {
			img8.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img8 + ".png"));
		}
		
		img9.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img9 + ".png"));
		if (wInfo.img10.equals("99")){
			img10.setVisibility(View.GONE);
		}else {
			img10.setImageBitmap(getImageFromAssetsFile("weathericon/b_" + wInfo.img10 + ".png"));
		}
		
	}
	
	public int matchDayofWeek(String day_of_week) {

		if (day_of_week.equals("星期一")) {
			return 1;
		} else if (day_of_week.equals("星期二")) {
			return 2;
		} else if (day_of_week.equals("星期三")) {
			return 3;
		} else if (day_of_week.equals("星期四")) {
			return 4;
		} else if (day_of_week.equals("星期五")) {
			return 5;
		} else if (day_of_week.equals("星期六")) {
			return 6;
		} else {
			return 7;
		}

	}

	public String[] weekDays(int i) {
		String[] days = null;
		switch (i) {
		case 1:
			days = new String[] { "周二", "周三", "周四", "周五" };
			break;
		case 2:
			days = new String[] { "周三", "周四", "周五", "周六" };
			break;
		case 3:
			days = new String[] { "周四", "周五", "周六", "周日" };
			break;
		case 4:
			days = new String[] { "周五", "周六", "周日", "周一" };
			break;
		case 5:
			days = new String[] { "周六", "周日", "周一", "周二" };
			break;
		case 6:
			days = new String[] { "周日", "周一", "周二", "周三" };
		case 7:
			days = new String[] { "周一", "周二", "周三", "周四" };
			break;

		}
		return days;
	}

	private Bitmap getImageFromAssetsFile(String fileName)  {  
	      Bitmap image = null;  
	      AssetManager am = getResources().getAssets();  
	      try {  
	          InputStream is = am.open(fileName);  
	          image = BitmapFactory.decodeStream(is);  
	          is.close();  
	      }  catch (IOException e)   {  
	          e.printStackTrace();  
	      }  
	  
	      return image;  	  
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK){		
			Intent it=new Intent(ActivityWeatherNotice.this, MainActivity.class);
			startActivity(it);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
